package com.ycsyxt.guide;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ycs(15241496739 @ 163.com)
 * @date 2022/6/12
 **/
public class Class12 {
    public static void main(String[] args) {
        // 二叉树深度优先遍历
        BinaryNode root = new BinaryNode(1);
        root.left = new BinaryNode(2);
        root.right = new BinaryNode(3);
        root.left.left = new BinaryNode(4);
        root.left.right = new BinaryNode(5);
        root.right.left = new BinaryNode(6);
        root.right.right = new BinaryNode(7);
        System.out.println(dfs(root));
    }
    
    public static List<Integer> dfs(BinaryNode root) {
        LinkedList<BinaryNode> stack = new LinkedList<>();
        stack.push(root);
        List<Integer> result = new ArrayList<>();
        while (!stack.isEmpty()) {
            BinaryNode temp = stack.pop();
            result.add(temp.val);
            if (temp.right != null) {
                stack.push(temp.right);
            }
            if (temp.left != null) {
                stack.push(temp.left);
            }
        }
        return result;
    }
}

class BinaryNode {
    public int val;
    
    public BinaryNode left;
    
    public BinaryNode right;
    
    public BinaryNode() {}
    
    public BinaryNode(int val) {
        this.val = val;
    }
    
    public BinaryNode(int val, BinaryNode left, BinaryNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}